package grape.workflow.rest.flowable.ext;

import grape.base.rest.client.user.UserClient;
import grape.common.SpringContextHolder;
import grape.common.exception.runtime.RBaseException;
import org.flowable.common.engine.api.query.QueryProperty;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.User;
import org.flowable.idm.api.UserQuery;
import org.flowable.idm.engine.impl.UserQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yangwei
 * Created at 2020/6/8 20:39
 */
public class CustomUserQueryImpl extends UserQueryImpl {

    @Override
    public UserQuery orderBy(QueryProperty property) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public UserQuery orderBy(QueryProperty property, NullHandlingOnOrder nullHandlingOnOrder) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public UserQuery asc() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public UserQuery desc() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public User singleResult() {
        if (this.getId() != null) {
            UserClient userClient = SpringContextHolder.getBean(UserClient.class);
            grape.base.service.user.po.User userClientUserById = userClient.getUserById(this.getId());
            return convertUser(userClientUserById);
        }

        return null;
    }

    @Override
    public List<User> list() {
        if (this.getIds() != null) {
            UserClient userClient = SpringContextHolder.getBean(UserClient.class);
            List<grape.base.service.user.po.User> userClientUserByIds = userClient.getUserByIds(this.getIds());
            if (userClientUserByIds != null) {
                return userClientUserByIds.stream().map(this::convertUser).collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<User> listPage(int firstResult, int maxResults) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public long count() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    private User convertUser(grape.base.service.user.po.User user){
        UserEntityImpl userEntity = new UserEntityImpl();
        userEntity.setId(user.getId());
        userEntity.setFirstName(user.getNickname());
        userEntity.setDisplayName(user.getNickname());
        return userEntity;
    }
}
